# AMAR ET AL. - COUNTERING MISINFORMATION EARLY (2025)
## REPLICATION FILE: 02_load_packages_and_data.R
### This script loads the necessary packages and processed data for the analysis.
# ----
# Load required packages ----
pacman::p_load(tidyverse, broom, readr, gtsummary, scales, srvyr, survey, data.table, margins, 
       xtable, ivreg, forcats, ggsignif, emmeans, clubSandwich, kableExtra, ggpubr, 
       here, mice, lmtest, tidysdm, cowplot, stringr, lubridate, haven, readxl)

options(scipen=999)

# Set seed for reproducibility ----
set.seed(3.14)

# Load main data frames ----
# Load data and ensure DK/refused responses are coded as NA
# Main df
bimli <- read_rds("data/cleaned/merged.rds") %>%
  mutate(across(gender:science, \(x) if_else(x == "Don't know/Refused", NA, x)),
         across(misinfo_threat1:engagement_behavior2, \(x) if_else(abs(x) %in% c(88, 99), NA, x)))

# Load svydesign
bimli_svy <- read_rds("data/cleaned/merged_svy.rds")

# Remove don't know/refused, create new svydesign
bimli_svy_dkr.rm <- bimli_svy %>% 
  mutate(across(gender:science, \(x) if_else(x == "Don't know/Refused", NA, x)),
         across(misinfo_threat1:engagement_behavior2, \(x) if_else(abs(x) %in% c(88, 99), NA, x))) %>%
  mutate(library_spillover_pre = paste0(library_id, "_", spillover_pre))

# Load DVs 
dv_labs <- read_csv2("data/raw/dvs/dv_labels.csv", show_col_types = FALSE)

# Edit DVs ----
## Add secondary outcomes ----
dv_labs$secondary_outcome <- FALSE
dv_labs$mechanism <- FALSE

# Secondary outcomes
new_dvs <- list(
  list(dv = "engagement_behavior_pay_comb", label = "Price to Participate", idx = "Engagement Behavior", subidx = NA, type = "question", secondary_outcome = TRUE, mechanism = FALSE),
  list(dv = "engagement_behavior_pay_no_price", label = "Participate at no Price", idx = "Engagement Behavior", subidx = NA, type = "question", secondary_outcome = TRUE, mechanism = FALSE),
  list(dv = "civic_participation", label = "Civic Participation Index", idx = "Civic Participation", subidx = NA, type = "idx", secondary_outcome = TRUE, mechanism = FALSE),
  list(dv = "civic_participation_collaborate", label = "Work with Others", idx = "Civic Participation", subidx = NA, type = "question", secondary_outcome = TRUE, mechanism = FALSE),
  list(dv = "civic_participation_rally", label = "Attend Rally", idx = "Civic Participation", subidx = NA, type = "question", secondary_outcome = TRUE, mechanism = FALSE),
  list(dv = "civic_participation_party_member", label = "Party Member", idx = "Civic Participation", subidx = NA, type = "question", secondary_outcome = TRUE, mechanism = FALSE),
  list(dv = "civic_participation_volunteer", label = "Volunteer", idx = "Civic Participation", subidx = NA, type = "question", secondary_outcome = TRUE, mechanism = FALSE),
  list(dv = "mechanism_logic", label = "Rely on Logic", idx = "Decision-Making", subidx = NA, type = "question", secondary_outcome = FALSE, mechanism = TRUE),
  list(dv = "mechanism_intuition", label = "Rely on Intuition", idx = "Decision-Making", subidx = NA, type = "question", secondary_outcome = FALSE, mechanism = TRUE),
  list(dv = "mechanism_others", label = "Rely on Others", idx = "Decision-Making", subidx = NA, type = "question", secondary_outcome = FALSE, mechanism = TRUE),
  list(dv = "mechanism_emotions", label = "Rely on Emotions", idx = "Decision-Making", subidx = NA, type = "question", secondary_outcome = FALSE, mechanism = TRUE),
  list(dv = "cues_index", label = "Cues", idx = "Source Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE))

# Add secondary DVs to df
dv_labs <- bind_rows(dv_labs, new_dvs)

# Change cues data
dv_labs <- dv_labs %>%
  filter(!dv %in% c("cues_good", "cues_bad"))

# Change order of idx
dv_labs <- dv_labs %>%
  mutate(idx = factor(idx, levels = c(
    "Accuracy Discernment", 
    "Sharing Discernment", 
    "Source Discernment", 
    "Health Preferences", 
    "Engagement Attitude", 
    "Engagement Behavior", 
    "Awareness",
    "Civic Participation",
    "Decision-Making"
  )))

## Create main indices df ----
dv_labs_endline_main <- dv_labs %>%
  filter(type %in% c("idx") & secondary_outcome == FALSE & mechanism == FALSE)

## Follow-up outcomes ----
# Separate df for follow-up outcomes
follow_up_dvs <- list(
  list(dv = "follow_accuracy_discernment", label = "Index", idx = "Accuracy Discernment", subidx = NA, type = "idx", secondary_outcome = FALSE, mechanism = FALSE, follow_up = TRUE),
  list(dv = "follow_accuracy_discernment_political", label = "Index", idx = "Political Discernment", subidx = NA, type = "idx", secondary_outcome = FALSE, mechanism = FALSE, follow_up = TRUE),
  list(dv = "follow_source_discernment", label = "Index", idx = "Source Discernment", subidx = NA, type = "idx", secondary_outcome = FALSE, mechanism = FALSE, follow_up = TRUE),
  list(dv = "follow_accuracy_true", label = "Accuracy of True Statements", idx = "Accuracy Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE, follow_up = TRUE),
  list(dv = "follow_accuracy_false", label = "Inaccuracy of False Statements", idx = "Accuracy Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE, follow_up = TRUE),
  list(dv = "follow_discernment_political_true", label = "Accuracy of True Statements", idx = "Political Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE, follow_up = TRUE),
  list(dv = "follow_discernment_political_false", label = "Inaccuracy of False Statements", idx = "Political Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE, follow_up = TRUE),
  list(dv = "follow_source_discern_specific_good", label = "Trust Reliable Sources", idx = "Source Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE, follow_up = TRUE),
  list(dv = "follow_source_discern_specific_bad", label = "Distrust Unreliable Sources", idx = "Source Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE, follow_up = TRUE))

follow_up_dvs <- as.data.frame(bind_rows(follow_up_dvs))

## Guardian outcomes ----
guardian_dvs <- list(
  # Endline
  #list(dv = "guardian_accuracy_discernment", label = "Index", idx = "Accuracy Discernment", subidx = NA, type = "idx", secondary_outcome = FALSE, mechanism = FALSE, follow = FALSE),
  #list(dv = "guardian_accuracy_true", label = "Accuracy of True Statements", idx = "Accuracy Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE, follow = FALSE),
  #list(dv = "guardian_accuracy_false", label = "Inaccuracy of False Statements", idx = "Accuracy Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE, follow = FALSE),
  list(dv = "guardian_ayurveda", label = "Ayurveda Ineffective", idx = "Traditional Medicine", subidx = NA, type = "idx", secondary_outcome = FALSE, mechanism = FALSE, follow = FALSE),
  # Follow-up endline
  # Accuracy Discernment
  list(dv = "follow_guardian_accuracy_discernment", label = "Index", idx = "Accuracy Discernment", subidx = NA, type = "idx", secondary_outcome = FALSE, mechanism = FALSE, follow = TRUE),
  list(dv = "follow_guardian_accuracy_true", label = "Accuracy of True Statements", idx = "Accuracy Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE, follow = TRUE),
  list(dv = "follow_guardian_accuracy_false", label = "Inaccuracy of False Statements", idx = "Accuracy Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE, follow = TRUE),
  # Traditional medicine
  # list(dv = "follow_guardian_ayurveda_effective", label = "Ayurveda Effective", idx = "Traditional Medicine", subidx = NA, type = "idx", secondary_outcome = FALSE, mechanism = FALSE, follow = TRUE),
  # Political Discernment
  list(dv = "follow_guardian_accuracy_discernment_political", label = "Index", idx = "Political Discernment", subidx = NA, type = "idx", secondary_outcome = FALSE, mechanism = FALSE, follow = TRUE),
  list(dv = "follow_guardian_discernment_political_true", label = "Accuracy of True Statements", idx = "Political Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE, follow = TRUE),
  list(dv = "follow_guardian_discernment_political_false", label = "Inaccuracy of False Statements", idx = "Political Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE, follow = TRUE),
  # Source discernment
  list(dv = "follow_guardian_source_discernment", label = "Index", idx = "Source Discernment", subidx = NA, type = "idx", secondary_outcome = FALSE, mechanism = FALSE, follow = TRUE),
  list(dv = "follow_guardian_source_discern_specific_good", label = "Trust Reliable Sources", idx = "Source Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE, follow = TRUE),
  list(dv = "follow_guardian_source_discern_specific_bad", label = "Distrust Unreliable Sources", idx = "Source Discernment", subidx = NA, type = "subidx", secondary_outcome = FALSE, mechanism = FALSE, follow = TRUE))

guardian_dvs <- as.data.frame(bind_rows(guardian_dvs))

# END of 02_load_packages_and_data.R ----
